<template>
  <el-button
    style="float: right"
    type="primary"
    @click="openDialog(false)"
  >+ 添加类别</el-button>
</template>

<script>


export default {
  data() {
    return {
      dialogVisible: false,
      dialogType: 'add', // 对话框类型：'add' 或 'edit'

    };
  },

  methods: {
    openDialog(isEdit = false, dept = null) {
      this.dialogVisible = true;
      this.dialogType = isEdit ? 'edit' : 'add';
      if (isEdit && dept) {
        this.dept = { ...dept };
      } else {
        this.dept = {};
      }
    },

    async handleSave() {
      try {
        let result;
        if (this.dept.id) {
          result = await update(this.dept);
        } else {
          result = await add(this.dept);
        }
        if (result.data.code === 1) {
          this.$message.success(`恭喜你，${this.dialogType === 'add' ? '新增' : '编辑'}成功`);
          this.init();
        } else {
          this.$message.error(result.data.msg);
        }
      } catch (error) {
        this.$message.error('操作失败，请重试');
      } finally {
        this.dialogVisible = false;
        this.dept = {}; // 清空表单
      }
    },

    async init() {
      const result = await findAlldept();
      if (result.data.code === 1) {
        this.tableData = result.data.data;
      }
    },
  },

  mounted() {
    this.init();
  },
};
</script>

<style scoped>

</style>
